package com.sfddata.musicpillow.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfddata.musicpillow.user.entity.SleepScoreTable;
import com.sfddata.musicpillow.user.entity.SleepScoreVo;
import com.sfddata.musicpillow.user.service.SleepScoreTableService;
import com.sfddata.musicpillow.user.mapper.SleepScoreTableMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author jiangyingou
* @description 针对表【sleep_score_table】的数据库操作Service实现
* @createDate 2023-08-07 14:53:56
*/
@Service
public class SleepScoreTableServiceImpl extends ServiceImpl<SleepScoreTableMapper, SleepScoreTable>
    implements SleepScoreTableService{

    @Override
    public SleepScoreVo compare(SleepScoreTable sleepScoreTable) {

        SleepScoreVo sleepScoreVo = new SleepScoreVo();
        BeanUtils.copyProperties(sleepScoreTable, sleepScoreVo);

        if (sleepScoreTable.getShowList() != null){
            String showList = sleepScoreTable.getShowList();
            String substring = showList.substring(1, showList.length()-1);
            String[] split = substring.split(", ");
            List<String> list = Arrays.stream(split).collect(Collectors.toList());
            sleepScoreVo.setShowList(list);
        }

        QueryWrapper<SleepScoreTable> sleepQW = new QueryWrapper<>();
        QueryWrapper<SleepScoreTable> pressQW = new QueryWrapper<>();
        QueryWrapper<SleepScoreTable> habitQW = new QueryWrapper<>();
        QueryWrapper<SleepScoreTable> scoreQW = new QueryWrapper<>();
        QueryWrapper<SleepScoreTable> avgScoreQW = new QueryWrapper<>();

        sleepQW.le("sleep", sleepScoreTable.getSleep());
        scoreQW.eq("age", sleepScoreTable.getAge());
        pressQW.le("press", sleepScoreTable.getPress());
        pressQW.eq("age", sleepScoreTable.getAge());
        habitQW.le("habit", sleepScoreTable.getHabit());
        habitQW.eq("age", sleepScoreTable.getAge());
        scoreQW.le("score", sleepScoreTable.getScore());
        scoreQW.eq("age", sleepScoreTable.getAge());
        avgScoreQW.eq("age", sleepScoreTable.getAge());

        List<SleepScoreTable> list = this.list(sleepQW);
        List<SleepScoreTable> list1 = this.list(pressQW);
        List<SleepScoreTable> list2 = this.list(habitQW);
        List<SleepScoreTable> list3 = this.list(scoreQW);
        List<SleepScoreTable> list4 = this.list(avgScoreQW);
        double count = this.count()*1.0;

        sleepScoreVo.setCompareSleep((list.size()*1.0/count));
        sleepScoreVo.setComparePress((list1.size()*1.0/count));
        sleepScoreVo.setCompareHabit((list2.size()*1.0/count));
        sleepScoreVo.setCompareSleepScore((list3.size()*1.0/count));
        Double avgScore = 0d;
        for (SleepScoreTable scoreTable : list4) {
            avgScore += scoreTable.getScore();
        }
        avgScore = avgScore/list4.size();
        sleepScoreVo.setAvgScore(avgScore);
        return sleepScoreVo;
    }

    @Override
    public List<SleepScoreTable> getByUid(String id) {

        QueryWrapper<SleepScoreTable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId", id);
        queryWrapper.orderByDesc("id");
        List<SleepScoreTable> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public SleepScoreVo setShowList(SleepScoreVo ssv) {


        return null;
    }

}




